#include "InputLayoutManager.h"

#include <D3D10.h>

#include "D3DErrorChecker.h"
#include "EffectManager.h"
#include "SpheresEffect.h"
#include "CubeMapSphereEffect.h"
#include "SkyEffect.h"

namespace D3D10InputLayouts
{
    ID3D10InputLayout* InputLayoutManager::mSpheresInputLayout = nullptr;
    ID3D10InputLayout* InputLayoutManager::mCubeMapSphereInputLayout = nullptr;
    ID3D10InputLayout* InputLayoutManager::mSkyInputLayout = nullptr;

    void InputLayoutManager::initAll(ID3D10Device * const device)
    {
        initSpheresInputLayout(device);
        initCubeMapSphereInputLayout(device);
        initSkyInputLayout(device);
    }

    void InputLayoutManager::destroyAll()
    {
        mSpheresInputLayout->Release();
        mCubeMapSphereInputLayout->Release();
        mSkyInputLayout->Release();
    }

    void InputLayoutManager::initSpheresInputLayout(ID3D10Device * const device)
    {
        D3D10_INPUT_ELEMENT_DESC vertexDesc[] = 
        {
            {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0},
            {"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
            {"TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
        };

        D3D10_PASS_DESC PassDesc;
        ID3D10EffectTechnique* technique = D3D10Effects::EffectManager::mSpheresEffect->mTechnique;
        technique->GetPassByIndex(0)->GetDesc(&PassDesc);
        HRESULT hr = device->CreateInputLayout(vertexDesc, 3, 
            PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &D3D10InputLayouts::InputLayoutManager::mSpheresInputLayout);

        D3D10Utils::ErrorChecker(hr);
    } 

    void InputLayoutManager::initCubeMapSphereInputLayout(ID3D10Device * const device)
    {
        D3D10_INPUT_ELEMENT_DESC vertexDesc[] = 
        {
            {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0},
            {"NORMAL",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
            {"TEXCOORD",  0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
        };

        D3D10_PASS_DESC PassDesc;
        ID3D10EffectTechnique* technique = D3D10Effects::EffectManager::mCubeMapSphereEffect->mTechnique;
        technique->GetPassByIndex(0)->GetDesc(&PassDesc);
        HRESULT hr = device->CreateInputLayout(vertexDesc, 3, 
            PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &D3D10InputLayouts::InputLayoutManager::mCubeMapSphereInputLayout);

        D3D10Utils::ErrorChecker(hr);
    } 

    void InputLayoutManager::initSkyInputLayout(ID3D10Device * const device)
    {
        D3D10_INPUT_ELEMENT_DESC vertexDesc[] = 
        {
            {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0},
        };

        D3D10_PASS_DESC PassDesc;
        ID3D10EffectTechnique* technique = D3D10Effects::EffectManager::mSkyEffect->mTechnique;
        technique->GetPassByIndex(0)->GetDesc(&PassDesc);
        HRESULT hr = device->CreateInputLayout(vertexDesc, 1, 
            PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &D3D10InputLayouts::InputLayoutManager::mSkyInputLayout);

        D3D10Utils::ErrorChecker(hr);
    }
}

